<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<body>
		<table id="reportControls">
			<tr>
				<td>
					<label>
						Report type:
					</label>
					<select dojoType="dijit.form.ComboBox" id="reportTypeSelect">
						<option>Users</option>
						<option>Projects</option>
						<option>Instruments</option>
						<script event="onChange" args="event" type="dojo/method">
 							var map = { Users : "/FinalProject/dtd/users.dtd",
										Projects : "/FinalProject/dtd/projects.dtd",
										Instruments : "/FinalProject/dtd/instruments.dtd"};
							dijit.byId( "reportTemplatesGrid").setQuery( { type : this.value});
							dojo.byId( "dtdLink").href = map[this.value];
						</script>
					</select>
					<div style="text-align:center;font-size:x-small">
						<a id="dtdLink" target="blank" href="/FinalProject/dtd/users.dtd">View report dtd file.</a>
					</div>
				</td>
				<td>
					<button dojoType="dijit.form.Button">
						Add new template
						<script event="onClick" args="event" type="dojo/method">
							var templateDialog = new dijit.Dialog( {
								title 	: "Add new template",
								id		: "templateDialog",
								href	: "/FinalProject/templates/xslt/xsltUploadTemplate.jsp",
								onCancel: function( event) {
									this.destroyRecursive( );
								}
							});
							templateDialog.show( );
						</script>
					</button>
				</td>
				<td>
					<button dojoType="dijit.form.Button">
						Delete report
						<script event="onClick" args="event" type="dojo/method">
							if ( dijit.byId( "reportTemplatesGrid").selection.getSelected( ).length == 0) {
								dijit.byId('toaster').setContent('Select template from the grid.', 'fatal');
							    dijit.byId('toaster').show();	
								return;
							}
							dojo.xhrPost( {
								url : "/FinalProject/app/xsltStorageManagement.xsl?xsltStorageOp=xslt_delete",
								content : {
									id : dijit.byId( "reportTemplatesGrid").selection.getSelected( )[0].id[0]
								},
								load : function( data, ioArgs) {
									if ( data == "ERROR") {
										dijit.byId('toaster').setContent('Backend error. Could not delete report template.', 'fatal');
									    dijit.byId('toaster').show();							
										return;
									}
							        dijit.byId('toaster').setContent('Report template deleted successefuly.', 'fatal');
							        dijit.byId('toaster').show();
							        dijit.byId( "reportTemplatesGrid").removeSelectedRows( );
								},
								error : function( ioArgs) {
									dijit.byId('toaster').setContent('Backend error. Could not delete report template.', 'fatal');
								    dijit.byId('toaster').show();							
									return;
								}
							});
						</script>
					</button>
				</td>
				<td>
					<button dojoType="dijit.form.Button">
						Generate report
						<script event="onClick" args="event" type="dojo/method">
							if ( dijit.byId( "reportTemplatesGrid").selection.getSelected( ).length==0) {
								dijit.byId( "toaster").setContent( "Select template first!", "fatal").show( );
								return;
							}
							var generatorDialog = new dijit.Dialog( {
								title 	: "Generate",
								id		: "generateDialog",
								href	: "/FinalProject/templates/xslt/reportGenerator.jsp",
								style	: "width:700px",
								onCancel: function( event) {
									this.destroyRecursive( );
								}
							});
							generatorDialog.show( );						
						</script>
					</button>
				</td>
			</tr>
		</table>
		<h2>
			Report templates list:
		</h2>
		<div class="GridContainer" id="reportTemplatesList" style="width:100%;height:400px">
			<div id="reportTemplatesGrid" style="width:100%;height:350px">
			</div>
		</div>	
	</body>
	<script type="text/javascript">
		dojo.addOnLoad( function( ) {
			var xsltStorageLayout = [
				{ field : "id", 		name : "Id", 			width : "auto"},
				{ field : "name", 		name : "Template name", width : "auto"},
				{ field : "type",		name : "Type",			width : "auto"},
				{ field : "postedBy", 	name : "Posted by",		width : "auto" }
			];
			var xsltStorageStore = new dojo.data.ItemFileWriteStore( { url : "/FinalProject/app/xsltStorageManagement.xsl?xsltStorageOp=xslt_list_all", clearOnClose: true, jsId:"projectsStore"});
			var grid = new dojox.grid.DataGrid( {
				structure : xsltStorageLayout,
				store	: xsltStorageStore,
				query : { type : "Users"}
			}, "reportTemplatesGrid");
			grid.startup( );
		});
	</script>
</html>